package com.xckj.zuoliao.presenter;

import androidx.annotation.NonNull;


import com.xckj.zuoliao.bean.UserLoginStatusBean;
import com.xckj.zuoliao.framework.BaseBean;
import com.xckj.zuoliao.framework.net.RetrofitManager;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.IUserStatusView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: 代码的搬运工
 * @Describe: 用户状态
 * @Date: 2024/4/1 18:06
 */
public class UserStatusPresenter {
    private final IUserStatusView userStatusView;

    public UserStatusPresenter(IUserStatusView userStatusView) {
        this.userStatusView = userStatusView;
    }

    /**
     * 获取用户登录状态
     *
     * @param uid 用户Id
     */
    public void getUserLoginStatus(String uid) {
        userStatusView.showLoading();
        Call<BaseBean<UserLoginStatusBean>> call = RetrofitManager.getInstance().getService().getUserLoginStatus(uid);
        call.enqueue(new Callback<BaseBean<UserLoginStatusBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<UserLoginStatusBean>> call, @NonNull Response<BaseBean<UserLoginStatusBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<UserLoginStatusBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        userStatusView.success(baseBean.data,baseBean.msg);
                    } else {
                        userStatusView.failed(baseBean.msg);
                    }
                } else {
                    userStatusView.failed("请求失败");
                }
                userStatusView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<UserLoginStatusBean>> call, @NonNull Throwable t) {

            }
        });
    }
}
